/*******************************************************************************
EntityResource.cpp

Notes:
*******************************************************************************/

#include "EntityResource.h"

//	LoadFromFile(std::string strFileName)
//		Loads entity information from a file
//			strFileName - the file to be loaded
void EntityResource::LoadFromFile(std::string strFileName)
{
	// Create a file stream and open it
	std::fstream file;
	file.open(strFileName.c_str());

	// If the file opened properly
	if(file.is_open())
	{
		// Create a string object
		std::string strLine;

		// While we haven't reached the end of the file...
		while(!file.eof())
		{
			// Get a line and remove the whitespace
			getline(file, strLine);
			RemoveWhiteSpace(strLine);

			// While the line is empty and we're not at the end of the file, get the next line
			while(strLine.empty() && !file.eof())
			{
				getline(file, strLine);
				RemoveWhiteSpace(strLine);
			} // while

			// Find the location of Class=
			size_t findLocation;
			findLocation = strLine.find("Class=") + 6;

			// If we found Class=
			if(findLocation != strLine.npos)
			{
				// Get the class type
				m_entityType = strLine.substr(findLocation);

				// While we haven't found {, get a new line and try again
				while(strLine.npos == strLine.find("{"))
				{
					getline(file, strLine);
					RemoveWhiteSpace(strLine);
				} // while

				// While we haven't found }...
				while(strLine.npos == strLine.find("}"))
				{
					// Get a new line
					getline(file, strLine);
					RemoveWhiteSpace(strLine);

					// Find the =
					findLocation = strLine.find("=");

					// Extract the index and value
					std::string index = strLine.substr(0, findLocation);
					std::string value = strLine.substr(findLocation + 1);

					// Store the value at the index
					m_entityData[index] = value;
				} // while
			} // if
		} // while
	} // if
} // LoadFromFile

//	GetProperty(std::string strIndex)
//		Retrieves the property at the given index
//			strIndex - The index to look up
std::string EntityResource::GetProperty(std::string strIndex)
{
	// If the index exists...
	if(!(m_entityData.find(strIndex) == m_entityData.end()))
	{
		// ... return the associated entity
		return m_entityData[strIndex];
	} // if

	// Return a blank string to indicate an error
	return "";
}

//	GetEntityType(void)
//		Returns the type of entity
std::string EntityResource::GetEntityType(void)
{
	// Return the stored entity type
	return m_entityType;
} // GetEntityType

//	RemoveWhiteSpace(std::string& str)
//		Removes spaces and tabs from a string
//			str - A reference to the string to be processed
void EntityResource::RemoveWhiteSpace(std::string& str)
{
	// Create a temporary string
	std::string temp;

	// Loop through each character in the string
	for(unsigned int chr = 0; chr < str.length(); chr++)
	{
		// If the current character is not a space or tab...
		if(str[chr] != ' ' && str[chr] != '\t')
		{
			// ... add the character to the temporary string
			temp += str[chr];
		} // if
	} // for

	// Copy the temporary (processed) string to the original
	str = temp;
} // RemoveWhiteSpace